Crate log_once

source ·
Expand description

Collection of helper macros for logging some events only once.

This crate provide macro in the log_once family (warn_once!, trace_once!, …); that only send a logging event once for every message. It rely and uses the logging infrastructure in the log crate; and is fully compatible with any logger implementation.

These macro will store the already seen messages in a static BTreeSet, and check if a message is in the set before sending the log event.

Examples

use log::info;
use log_once::{info_once, warn_once};

pub fn shave_the_yak(yaks: &[Yak]) {
    for yak in yaks {
        info!(target: "yak_events", "Commencing yak shaving for {yak:?}");

        loop {
            match find_a_razor() {
                Ok(razor) => {
                    // This will only appear once in the logger output for each razor
                    info_once!("Razor located: {razor}");
                    yak.shave(razor);
                    break;
                }
                Err(err) => {
                    // This will only appear once in the logger output for each error
                    warn_once!("Unable to locate a razor: {err}, retrying");
                }
            }
        }
    }
}

Re-exports

Macros

  • Logs a message once at the debug level.
  • Logs a message once at the error level.
  • Logs a message once at the info level.
  • Standard logging macro, logging events once for each arguments.
  • Logs a message once at the trace level.
  • Logs a message once at the warn level.

Enums

  • An enum representing the available verbosity levels of the logger.